/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * TablesPanel.java
 *
 * Created on Jul 12, 2012, 11:24:00 AM
 */

package com.piza.toolcenter.view.mybatis;

import com.piza.toolcenter.beans.TableConfig;
import com.piza.toolcenter.model.DBInfo;
import com.piza.toolcenter.operation.IWorkFlow;
import com.piza.toolcenter.operation.OperationTree;
import com.piza.toolcenter.operation.SessionContext;
import java.awt.event.MouseEvent;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.swing.DefaultListModel;
import javax.swing.event.MouseInputAdapter;

/**
 *
 * @author cwb
 */
public class TablesPanel extends javax.swing.JPanel implements IWorkFlow{

    private SessionContext context;
    private List<String> allTableList;
    private Map<String,TableConfig> selectedTables=new HashMap<String,TableConfig>();
    
    private DefaultListModel rightListModel=new DefaultListModel();
    /** Creates new form TablesPanel */
    public TablesPanel() {
        initComponents();
    }

    public void open(SessionContext context) {
        this.context=context;
        initData();
        
    }

    public boolean close() {

        this.context.addItem("SelectedTables", this.selectedTables);
        return true;
    }

    
    private void initData(){
        this.allTableList=OperationTree.getDBTables(((DBInfo)context.getItem("CurrentDBInfo")));
        DefaultListModel listModel=new DefaultListModel();
        
        if(this.allTableList==null || this.allTableList.size()==0){
            return;
        }
        
        for(String tbName:this.allTableList){
            listModel.addElement(tbName);
        }
        this.leftList.setModel(listModel);
        this.leftList.addMouseListener(new LeftMouseListener());
        
        selectedTables=new HashMap<String,TableConfig>();
        rightListModel=new DefaultListModel();
        this.rightList.setModel(rightListModel);
        this.rightList.addMouseListener(new RightMouseListener());
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        bindingGroup = new org.jdesktop.beansbinding.BindingGroup();

        tableConfig = new com.piza.toolcenter.beans.TableConfig();
        jScrollPane1 = new javax.swing.JScrollPane();
        leftList = new javax.swing.JList();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        generateKeyCheckBox = new javax.swing.JCheckBox();
        keyTextField = new javax.swing.JTextField();
        defaultButton = new javax.swing.JButton();
        jLabel5 = new javax.swing.JLabel();
        tableNameLabel = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        schemaLabel = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        domainTextField = new javax.swing.JTextField();
        jScrollPane2 = new javax.swing.JScrollPane();
        rightList = new javax.swing.JList();
        saveButton = new javax.swing.JButton();

        setPreferredSize(new java.awt.Dimension(750, 400));

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        leftList.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        leftList.setName("leftList"); // NOI18N
        jScrollPane1.setViewportView(leftList);

        jLabel1.setText("Tables");
        jLabel1.setName("jLabel1"); // NOI18N

        jLabel2.setText("Properties");
        jLabel2.setName("jLabel2"); // NOI18N

        jLabel3.setText("Selected");
        jLabel3.setName("jLabel3"); // NOI18N

        generateKeyCheckBox.setText("generatedKey");
        generateKeyCheckBox.setName("generateKeyCheckBox"); // NOI18N

        org.jdesktop.beansbinding.Binding binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, tableConfig, org.jdesktop.beansbinding.ELProperty.create("${generatedKey}"), generateKeyCheckBox, org.jdesktop.beansbinding.BeanProperty.create("selected"));
        bindingGroup.addBinding(binding);

        generateKeyCheckBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                generateKeyCheckBoxActionPerformed(evt);
            }
        });

        keyTextField.setEditable(false);
        keyTextField.setName("keyTextField"); // NOI18N

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, tableConfig, org.jdesktop.beansbinding.ELProperty.create("${keyColumn}"), keyTextField, org.jdesktop.beansbinding.BeanProperty.create("text"));
        bindingGroup.addBinding(binding);

        defaultButton.setText("Default");
        defaultButton.setName("defaultButton"); // NOI18N

        jLabel5.setText("Table Name:");
        jLabel5.setName("jLabel5"); // NOI18N

        tableNameLabel.setName("tableNameLabel"); // NOI18N

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, tableConfig, org.jdesktop.beansbinding.ELProperty.create("${tableName}"), tableNameLabel, org.jdesktop.beansbinding.BeanProperty.create("text"));
        bindingGroup.addBinding(binding);

        jLabel7.setText("Schema:");
        jLabel7.setName("jLabel7"); // NOI18N

        schemaLabel.setName("schemaLabel"); // NOI18N

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, tableConfig, org.jdesktop.beansbinding.ELProperty.create("${schema}"), schemaLabel, org.jdesktop.beansbinding.BeanProperty.create("text"));
        bindingGroup.addBinding(binding);

        jLabel9.setText("domainObjectName");
        jLabel9.setName("jLabel9"); // NOI18N

        domainTextField.setName("domainTextField"); // NOI18N

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, tableConfig, org.jdesktop.beansbinding.ELProperty.create("${domainObjectName}"), domainTextField, org.jdesktop.beansbinding.BeanProperty.create("text"));
        bindingGroup.addBinding(binding);

        jScrollPane2.setName("jScrollPane2"); // NOI18N

        rightList.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        rightList.setName("rightList"); // NOI18N
        jScrollPane2.setViewportView(rightList);

        saveButton.setText("save");
        saveButton.setName("saveButton"); // NOI18N
        saveButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                saveButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(21, 21, 21)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(jLabel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 244, Short.MAX_VALUE))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(60, 60, 60)
                        .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 151, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(117, 117, 117))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(defaultButton)
                                .addGap(34, 34, 34)
                                .addComponent(saveButton))
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel7)
                                    .addComponent(jLabel5)
                                    .addComponent(jLabel9)
                                    .addComponent(generateKeyCheckBox))
                                .addGap(18, 18, 18)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(domainTextField, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 121, Short.MAX_VALUE)
                                    .addComponent(tableNameLabel, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 72, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(schemaLabel, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 121, Short.MAX_VALUE)
                                    .addComponent(keyTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 114, javax.swing.GroupLayout.PREFERRED_SIZE))))
                        .addGap(70, 70, 70)))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 72, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 149, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jLabel5, jLabel7, jLabel9});

        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2)
                    .addComponent(jLabel3))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(15, 15, 15)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel5)
                            .addComponent(tableNameLabel))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel7)
                            .addComponent(schemaLabel))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel9)
                            .addComponent(domainTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(19, 19, 19)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(generateKeyCheckBox)
                            .addComponent(keyTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 153, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(defaultButton)
                            .addComponent(saveButton)))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED, 17, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jScrollPane2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 309, Short.MAX_VALUE)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 307, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addGap(38, 38, 38))
        );

        layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {generateKeyCheckBox, keyTextField});

        bindingGroup.bind();
    }// </editor-fold>//GEN-END:initComponents

    private void generateKeyCheckBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_generateKeyCheckBoxActionPerformed
        if(this.generateKeyCheckBox.isSelected()){
            this.keyTextField.setEditable(true);
        }else{
            this.keyTextField.setText("");
            this.keyTextField.setEditable(false);
        }
    }//GEN-LAST:event_generateKeyCheckBoxActionPerformed

    private void saveButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveButtonActionPerformed
        TableConfig tbConfig=this.tableConfig.copy();
        if(!this.selectedTables.containsKey(tbConfig.getTableName())){
            this.rightListModel.addElement(tbConfig.getTableName());
        }
        this.selectedTables.put(tbConfig.getTableName(), tbConfig);
        
        
    }//GEN-LAST:event_saveButtonActionPerformed

    private void clean(){
        this.domainTextField.setText("");
        tableNameLabel.setText("");
        schemaLabel.setText("");
        this.generateKeyCheckBox.setSelected(false);
        this.keyTextField.setText("");
        
    }
    class RightMouseListener extends MouseInputAdapter{

        @Override
        public void mouseClicked(MouseEvent e) {
            String tbName= rightList.getSelectedValue().toString();
          if(tbName==null){
              return;
          }
          if(selectedTables.containsKey(tbName)){
              showTableConfig(selectedTables.get(tbName));
          }
          
          
        }
        
    }
    
    private void showTableConfig(TableConfig tc){
        this.generateKeyCheckBox.setSelected(tc.isGeneratedKey());
        if(tc.isGeneratedKey()){
            this.keyTextField.setText(tc.getKeyColumn());
        }
        tableNameLabel.setText(tc.getTableName());
           schemaLabel.setText(tc.getSchema());
           domainTextField.setText(tc.getDomainObjectName());
    }
    
    class LeftMouseListener extends MouseInputAdapter{

        @Override
        public void mouseClicked(MouseEvent e) {
            
           String tbName= leftList.getSelectedValue().toString();
          if(tbName==null){
              return;
          }
          clean();
           tableNameLabel.setText(tbName);
//           tableConfig.setTableName(tbName);
           DBInfo dbInfo=(DBInfo)context.getItem("CurrentDBInfo");
           schemaLabel.setText(OperationTree.getSchema(dbInfo.getConnectUrl()));
//           tableConfig.setSchema(OperationTree.getSchema(dbInfo.getConnectUrl()));
           domainTextField.setText(OperationTree.convertTableName(tbName));
//           tableConfig.setDomainObjectName(OperationTree.convertTableName(tbName));
            
        }
        
        
        
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton defaultButton;
    private javax.swing.JTextField domainTextField;
    private javax.swing.JCheckBox generateKeyCheckBox;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTextField keyTextField;
    private javax.swing.JList leftList;
    private javax.swing.JList rightList;
    private javax.swing.JButton saveButton;
    private javax.swing.JLabel schemaLabel;
    private com.piza.toolcenter.beans.TableConfig tableConfig;
    private javax.swing.JLabel tableNameLabel;
    private org.jdesktop.beansbinding.BindingGroup bindingGroup;
    // End of variables declaration//GEN-END:variables

}
